﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using System.Security.Cryptography;

public partial class Admin_Ajax_QuanLy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["Methods"] != null)
        {
            switch (Request["Methods"])
            {
                case "ThemMoi":
                    themmoi();
                    break;
                case "CapNhat":
                    CapNhat();
                    break;
                case "GetInfo":
                    GetInfo();
                    break;
                case "delete":
                    Xoa();
                    break;
                case "DoiMatKhau":
                    DoiMatKhauAdmin();
                    break;
                case "ThongTinCaNhan":
                    ThongTinCaNhan();
                    break;
                case "DoiMatKhauUser":
                    DoiMatKhauUser();
                    break;
                default:
                    break;
            }
        }
    }

    public void themmoi()
    {
        string s = "";
        JavaScriptSerializer serial = new JavaScriptSerializer();
        if (Request.Params["info"] != null)
        {
            s = Request.Params["info"];
        }
        QuanLyINFO nguoidung = serial.Deserialize<QuanLyINFO>(s);

        if (QuanLyDAL.Insert(nguoidung) > 0)
        {
            Response.Write("{\"Status\": \"Success\"}");
        }
        else
        {
            Response.Write("{\"Status\": \"Error\",\"Mess\":\"Không thể thêm người dùng.\"}");
        }

        Response.End();
    }

    public void CapNhat()
    {
        string s = "";
        JavaScriptSerializer serial = new JavaScriptSerializer();
        if (Request.Params["info"] != null)
        {
            s = Request.Params["info"];
        }
        QuanLyINFO nguoidung = serial.Deserialize<QuanLyINFO>(s);

        if (QuanLyDAL.Update(nguoidung) > 0)
        {
            Response.Write("{\"Status\": \"Success\"}");
        }
        else
        {
            Response.Write("{\"Status\": \"Error\",\"Mess\":\"Không thể cập nhật.\"}");
        }

        Response.End();
    }

    public void GetInfo()
    {
        int ID = int.Parse(Request.Params["iID"]);
        JavaScriptSerializer serial = new JavaScriptSerializer();
        serial.RegisterConverters(new[] { new JSONConverter<QuanLyINFO>() });
        QuanLyINFO obj = QuanLyDAL.GetInfo(ID);
        Response.Write(serial.Serialize(obj));
        Response.End();
    }

    public void Xoa()
    {
        int ID = int.Parse(Request.Params["iID"]);
        if (QuanLyDAL.Delete(ID) > 0)
        {
            Response.Write("{\"Status\":\"OK\"}");
        }
        else
        {
            Response.Write("{\"Status\":\"Error\",\"Mess\":\"Không thể xóa!\"}");
        }
        Response.End();
    }

    public void DoiMatKhauAdmin()
    {
        MD5 md = MD5.Create();
        int id = 0;
        string pass = "";
        if (Request.Params["iID"] != null)
        {
            id = int.Parse(Request.Params["iID"]);
        }
        if (Request.Params["password"] != null)
        {
            pass = Request.Params["password"];
        }
        if (QuanLyDAL.DoiMatKhau(id, pass) > 0)
        {
            Response.Write("{\"Status\": \"Success\"}");
        }
        else
        {
            Response.Write("{\"Status\": \"Error\",\"Mess\":\"Không thể đổi mật khẩu.\"}");
        }

        Response.End();
    }

    public void ThongTinCaNhan()
    {
        string s = "";
        JavaScriptSerializer serial = new JavaScriptSerializer();
        if (Request.Params["info"] != null)
        {
            s = Request.Params["info"];
        }
        QuanLyINFO nguoidung = serial.Deserialize<QuanLyINFO>(s);

        QuanLyINFO user = (QuanLyINFO)Session["Admin"];
        nguoidung.MaQL = user.MaQL;
        nguoidung.Email = user.Email;
        nguoidung.TrangThai = user.TrangThai;
        if (QuanLyDAL.Update(nguoidung) > 0)
        {
            Response.Write("{\"Status\": \"Success\"}");
        }
        else
        {
            Response.Write("{\"Status\": \"Error\",\"Mess\":\"Không thể cập nhật.\"}");
        }

        Response.End();
    }

    public void DoiMatKhauUser()
    {
        MD5 md = MD5.Create();
        QuanLyINFO user = (QuanLyINFO)Session["Admin"];
        string pass = "";
        string newpass = "";
        if (Request.Params["password"] != null)
        {
            pass = Request.Params["password"];
        }
        if (Request.Params["newpassword"] != null)
        {
            newpass = Request.Params["newpassword"];
        }
        QuanLyINFO obj = QuanLyDAL.GetInfo(user.MaQL);
        if (!obj.MatKhau.Equals(pass))
        {
            Response.Write("{\"Status\": \"Error\",\"Mess\":\"Mật khẩu hiện tại không đúng.\"}");
            return;
        }
        if (QuanLyDAL.DoiMatKhau(user.MaQL, newpass) > 0)
        {
            Response.Write("{\"Status\": \"Success\"}");
        }
        else
        {
            Response.Write("{\"Status\": \"Error\",\"Mess\":\"Không thể đổi mật khẩu.\"}");
        }

        Response.End();
    }
}